﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using trangbaocao.Models;
using System.Web.Security;
//using System.Web.SessionState;
namespace trangbaocao.Controllers
{
    //[Authorize]
    //[SessionState(SessionStateBehavior.Required)]
    public class HomeController : Controller
    {
        //public ActionResult SessionExpired()
        //{
        //    Session.Abandon();
        //    FormsAuthentication.SignOut();
        //    return RedirectToAction("Index");
        //}
        //public class SessionExpireFilterAttribute : ActionFilterAttribute
        //{
        //    public override void OnActionExecuting(ActionExecutingContext filterContext)
        //    {
        //        HttpContext ctx = System.Web.HttpContext.Current;

        //        // check if session is supported 
        //        if (ctx.Session != null && ctx.Session.IsNewSession)
        //        {
        //            // If it says it is a new session, but an existing cookie exists, then it must 
        //            // have timed out 
        //            string sessionCookie = ctx.Request.Headers["Cookie"];
        //            if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
        //            {
        //                FormsAuthentication.SignOut();
        //                ctx.Response.Redirect("~/Home/Index");
        //            }
        //        }

        //        base.OnActionExecuting(filterContext);
        //    }
        //}
        
        public ActionResult Index()
        {
            if (Request.IsAuthenticated)
                ViewBag.status = false;
            else
                ViewBag.status = true;           
            return View();
            
        }
        [HttpPost]
        public ActionResult Index(UserModel us, string returnUrl)
        {
            
            //var errors = ModelState.Values.SelectMany(v => v.Errors);
            if (ModelState.IsValid)
            {
                if (UserModel.IsValid(us.user_name, us.password) != 0)
                {
                    UserModel userModel = new UserModel();
                    PermissionDetailModel PermissionDetailModel = new PermissionDetailModel();

                    List<UserModel> user = userModel.getUserByName(us.user_name);
                    int quyen_id=0;
                    foreach (UserModel u in user)
                    {
                        quyen_id = u.quyen;
                        Session["quyen_id"] = u.quyen;
                        Session["trungtam_id"] = u.main_stations_id;
                        Session["user_name"] = u.user_name;
                    }
                    List<PermissionDetailModel> permission_detail = PermissionDetailModel.getPermissionDetail(quyen_id);
                    //int[] quyen_id=new int[100];
                    Dictionary<int, int> quyen =  new Dictionary<int, int>();
                    foreach (PermissionDetailModel value in permission_detail)
                    {
                        quyen[value.menu_id] = value.menu_id;
                    }
                    Session["quyen"] = quyen;
                    //Dictionary<int, int> test = (Dictionary<int, int>)Session["quyen"];
                    
                    FormsAuthentication.SetAuthCookie(us.user_name, us.RememberMe); 
                    return RedirectToAction("Index");                    
                }
                else
                {
                    //return Content("<script language='javascript' type='text/javascript'>alert('b');</script>");
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }            
            TempData["msg"] = "Tên đăng nhập hoặc mật khẩu không chính xác!";
            return RedirectToAction("Index");
            //return View(us);            
            
        }
        public ActionResult About()
        {
            return View();
        }
    }
}
